/*** server ***/
package comm;

import java.net.*;
import java.io.*;
import main.Main;
import util.Server;

public class Communication {

    static public int teste;

    private boolean isConnected;
    static private boolean isConnectedToServer;
    private static ServerSocket serverSocket = null;
    public boolean listening = true;
    private static Socket kkSocket = null;
    public static PrintWriter out = null;
    public static BufferedReader in = null;
    public static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    public static String fromServer;
    public static String fromUser;

    public static boolean isConnectedToServer() {
        return isConnectedToServer;
    }

    public void connect() {
        try {
            serverSocket = new ServerSocket(Main.getPort());
            System.err.println("Listenning to port: " + Main.getPort());
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + Main.getPort());
            System.exit(-1);
        }
        isConnected = true;

        try {
            while (listening) {
                new KKMultiServerThread(serverSocket.accept()).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void connectToServer(String server, int port) {
        //KnockKnockClient.execute();
        if (isConnectedToServer) {
            return;
        }
        try {
            System.out.println("Conectando com o servidor");
            kkSocket = new Socket(server, port);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            return;
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            return;
        }
        isConnectedToServer = true;
    }

    public void disconnect() {
        try {
            serverSocket.close();
            isConnected = false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void disconnectFromServer() {
        try {
            out.close();
            in.close();
            stdIn.close();
            kkSocket.close();
            isConnectedToServer = false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String read() {
        try {
            fromServer = in.readLine();
            if (fromServer != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye.")) { //tentar alterar parada
                    System.out.println("Passou aqui.");
                    disconnectFromServer();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fromServer;
    }

    public static String writeAndRead(String data) {
        try {
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye.")) {
                    break;
                }
                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fromServer;
    }

    public static void write(String data) {

        fromUser = data;

        out.println(fromUser);
        if (fromUser != null) {
            System.out.println("Client: " + fromUser);
        }


    }
}
